package cc.eddic.pratice.programmingparadigms.emp01array.impl;

import cc.eddic.pratice.programmingparadigms.Employee;
import cc.eddic.pratice.programmingparadigms.emp01array.EmployeeReaderService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.ReadSheet;

import java.util.ArrayList;
import java.util.List;

public class ExcelReaderCxmImpl implements EmployeeReaderService {
    @Override
    public Employee[] read(String fileName) {
        List<Employee> employeelist=new ArrayList<>();
        ExcelReader excelReader = EasyExcel.read(fileName, Employee.class, new AnalysisEventListener<Employee>() {
            @Override
            public void invoke(Employee employee, AnalysisContext analysisContext) {
                employeelist.add(employee);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).build();

        ReadSheet readSheet= EasyExcel.readSheet("数据").build();
        excelReader.read(readSheet);
        excelReader.finish();
        Employee[] employees=new Employee[employeelist.size()];
        employeelist.toArray(employees);
        return employees;
    }
}
